"""我们定义公告板相关的可靠性."""
from typing_extensions import Annotated
from fastapi import HTTPException, Path, UploadFile, File, Form

from core.database import db_session
from core.models import Board
from lib.common import dynamic_create_write_table


def get_board(
    db: db_session,
    bo_table: str = Path(..., title="公告板表名", description="公告板表名"),
) -> Board:
    """
    查询公告栏信息。
    """
    board = db.get(Board, bo_table)
    if not board:
        raise HTTPException(status_code=404, detail="这是一个不存在的公告板.")
    return board


def get_write(
    db: db_session,
    bo_table: str = Path(..., title="公告板表名", description="公告板表名"),
    wr_id: int = Path(..., title="文章ID", description="编写idi"),
):
    """
    查看帖子信息。
    """
    write_model = dynamic_create_write_table(bo_table)
    write = db.get(write_model, wr_id)
    if not write:
        raise HTTPException(status_code=404, detail="这是一个不存在的帖子.")

    return write


def arange_file_data(
    file1: Annotated[UploadFile, File(title="附件1")] = None,
    file2: Annotated[UploadFile, File(title="附件2")] = None,
    file_content1: Annotated[str, Form(title="附件1 内容")] = None,
    file_content2: Annotated[str, Form(title="附件2 内容")] = None,
    file_del1: Annotated[int, Form(title="附件1 是否删除")] = None,
    file_del2: Annotated[int, Form(title="附件2 是否删除")] = None,
) -> dict:
    """以字典的形式返回上传文件的数据."""
    return {
        "files": [file1, file2],
        "file_contents": [file_content1, file_content2],
        "file_dels": [file_del1, file_del2],
    }